package org.example.cloud.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.cloud.entities.Pay;
import org.example.cloud.entities.dto.PayDTO;
import org.example.cloud.exception.CustomException;
import org.example.cloud.service.PayService;
import org.example.cloud.util.ResultData;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 宋孟林
 * @description
 * @date: 2025/4/4 20:08
 */
@RestController
@RequestMapping("/pay")
@Slf4j
@Tag(name =  "支付微服务模块", description = "支付crud")
public class PayController {
    @Resource
    private PayService payService;

    @Operation(description = "返回添加订单的数量")
    @PostMapping("/add")
    public ResultData<String> addPay(@RequestBody Pay pay){
        log.info("支付服务提供者1");
        log.info("pay 对象: {}", pay.toString());
        int i = payService.add(pay);
        return ResultData.success("插入成功" + i);
    }

    @Operation(description = "返回删除订单的数量")
    @DeleteMapping("/del/{id}")
    public ResultData<Integer> deletePay(@PathVariable("id") Integer id){
        int i = payService.delete(id);
        return ResultData.success(i);
    }

    @Operation(description = "返回更新订单的数量")
    //putMapping代表的是接收的请求的类型
    @PutMapping("/update")
    public ResultData<String> updatePay(@RequestBody PayDTO payDTO){
        Pay pay = new Pay();
        BeanUtils.copyProperties(payDTO, pay);
        int i = payService.update(pay);
        return ResultData.success("更新成功，数量为 " + i);
    }

    @Operation(description = "返回根据id查询到的订单对象")
    @GetMapping("/get/{id}")
    public ResultData<Pay> getById(@PathVariable("id") Integer id){
        if (id < 0){
            throw new CustomException("id不能为负数");
        }
        Pay pay = payService.getById(id);
        return ResultData.success(pay);
    }

    @Operation(description = "返回所有订单列表")
    @GetMapping("/getAll")
    public ResultData<List<PayDTO>> selectAll(){
        log.info("当前是支付微服务1");
        log.info("订单逻辑正在执行");
        log.info("查询所有订单和支付信息执行");
        List<Pay> payList = payService.getAll();
        List<PayDTO> payDTOList;
        payDTOList = payList.stream().map(item -> {
            PayDTO payDTO = new PayDTO();
            BeanUtils.copyProperties(item, payDTO);
            return payDTO;
        }).collect(Collectors.toList());
        return ResultData.success(payDTOList);
    }

}
